<template>

  <el-form ref="activityRefFrom" :model="activityQuery" label-width="110px" :rules="activityRules">
    <el-form-item label="负责人" prop="ownerId">
      <el-select v-model="activityQuery.ownerId" placeholder="请选择" class="width">
        <el-option
            v-for="item in ownerOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
      </el-select>
    </el-form-item>

    <el-form-item label="活动名称" prop="name">
      <el-input v-model="activityQuery.name"/>
    </el-form-item>

    <el-form-item label="开始时间" prop="startTime">
      <el-date-picker
          v-model="activityQuery.startTime"
          type="datetime"
          placeholder="请选择开始时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 100%;"/>
    </el-form-item>

    <el-form-item label="结束时间" prop="endTime">
      <el-date-picker
          v-model="activityQuery.endTime"
          type="datetime"
          placeholder="请选择结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 100%;"/>
    </el-form-item>

    <el-form-item label="活动预算" prop="cost">
      <el-input v-model="activityQuery.cost"/>
    </el-form-item>

    <el-form-item label="活动描述" prop="description">
      <el-input
          v-model="activityQuery.description"
          style="width: 100%"
          :rows="6"
          type="textarea"
          placeholder="请输入活动描述"/>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="activitySubmit">提 交</el-button>
      <el-button @click="goBack">返 回</el-button>
    </el-form-item>

  </el-form>

</template>

<script>
import {doGet, doPost, doPut} from "../http/httpRequest.js";
import {messageTip} from "../util/util.js";

export default {
  name: "ActivityRecordView",

  //注入
  inject: ['reload'],

  data() {
    return {
      //市场活动表单，初始值是空
      activityQuery: {},
      //市场活动表单验证规则
      activityRules: {
        ownerId: [
          {required: true, message: '请选择负责人', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入活动名称', trigger: 'blur'}
        ],
        startTime: [
          {required: true, message: '请选择开始时间', trigger: 'blur'}
        ],
        endTime: [
          {required: true, message: '请选择结束时间', trigger: 'blur'}
        ],
        cost: [
          {required: true, message: '请输入活动预算', trigger: 'blur'},
          {pattern: /^(0|[1-9]\d*)(\.\d{1,2})?$/, message: '活动预算必须是正整数或两位小数', trigger: 'blur'},
        ],
        description: [
          {required: true, message: '请输入活动描述', trigger: 'blur'},
          {min: 5, max: 255, message: '活动描述长度在5到255个字符之间', trigger: 'blur'}
        ]


      },
      //负责人的下拉选项，初始值是空
      ownerOptions: [{}],
    }
  },

  mounted() {
    this.loadOwner();
    //加载要编辑的数据（由于录入和编辑公用同一个页面，所以要判断是编辑还是录入）
    this.loadActivity();
  },

  methods: {
    //加载负责人
    loadOwner() {
      doGet("/api/owner", {}).then(resp => {
        if (resp.data.code === 200) {
          this.ownerOptions = resp.data.data;
        }
      })
    },

    //返回
    goBack() {
      this.$router.go(-1);
    },

    //市场活动提交
    activitySubmit() {
      let formData = new FormData();
      for (let field in this.activityQuery) {
        if (this.activityQuery[field]){//this.activityQuery[field]字段有值，不是空，才拼接传后端
          formData.append(field, this.activityQuery[field]);
        }
      }
      this.$refs.activityRefFrom.validate((isValid) => {
        if (this.activityQuery.id > 0) { /*编辑*/
          doPut("/api/activity", formData).then(resp => {
            if (resp.data.code === 200) {
              messageTip("编辑成功", "success");
              //跳转到市场活动列表页
              this.$router.push("/dashboard/activity")
            } else {
              messageTip("编辑失败", "error");
            }
          })
        } else {/*新增*/
          doPost("/api/activity", formData).then(resp => {
            if (resp.data.code === 200) {
              messageTip("提交成功", "success");
              //跳转到市场活动列表页
              this.$router.push("/dashboard/activity")
            } else {
              messageTip("提交失败", "error");
            }
          })
        }

      })
    },

    //加载要编辑的市场活动数据
    loadActivity() {
      let id = this.$route.params.id;
      if (id) {
        //id存在，说明是编辑
        doGet("/api/activity/" + id, {}).then(resp => {
          if (resp.data.code === 200) {
            this.activityQuery = resp.data.data;
          }
        })
      }
    }

  }
}
</script>

<style scoped>
.width {
  width: 100%;
}
</style>